package com.traffic.between.annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gxhl.api.core.model.GDPResponse;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.managent.service.order.bean.OrderBean;

/**
 * 类名称: ProductChoose 类描述: 通道选择 备注: 可能根据不同的渠道去调用方法
 */
public class ProvinderChoose {
	private static Logger logger = Logger.getLogger(ProvinderChoose.class);
	public static final ProvinderChoose service = new ProvinderChoose();

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void orderInvoke(String channel) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, InstantiationException {
		logger.info("ProvinderChoose orderInvoke() channel = "+channel);
		Object adaptor = InterfaceProviderPlugin.getAdaptorByChannel(channel);
		Class adaptorClass = adaptor.getClass();
		Method m2 = adaptorClass.getDeclaredMethod("order");
		OrderBean order = OrderInfoContext.getContext();
		if (null !=m2) {
			try {
			  m2.invoke(adaptor);
			} catch (Exception e) {
				logger.info("订单号为："+order.getIds()+"往上游:["+channel+" ]订购返回数据发生异常："+e.getMessage());
				order.setStatus("10");//作不继续往上查询处理。在队列中作受理中处理
				order.setMessage(e.getMessage());
				order.setErrorMsg(e.getMessage());
				BsmOrderResultExecutor.service.setOrder(order);
			}
			
		}else {
			logger.info("ProvinderChoose orderInvoke() Method = "+m2);
		}
		
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public GDPResponse orderInvoke(String channel, String method, Map params) throws NoSuchMethodException,
			IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
		logger.info("ProvinderChoose orderInvoke() channel = "+channel);
		Object adaptor = InterfaceProviderPlugin.getAdaptorByChannel(channel);
		Class adaptorClass = adaptor.getClass();
		Method m2 = adaptorClass.getDeclaredMethod("order");
		return (GDPResponse) m2.invoke(adaptor);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void statusInvoke(String channel) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, InstantiationException {
		logger.info("ProvinderChoose statusInvoke() channel = "+channel);
		Object adaptor = InterfaceProviderPlugin.getAdaptorByChannel(channel);
		OrderBean order = OrderInfoContext.getContext();
		Class adaptorClass = adaptor.getClass();
		Method m2 = adaptorClass.getDeclaredMethod("status");
		if (null!=m2) {
			try {
				m2.invoke(adaptor);	
		 } catch (Exception e) {
			logger.info("订单号为："+order.getIds()+"往上游:["+channel+"]查询数据返回发生异常："+e.getMessage()+",该订单重新查询状态");
			order.setStatus("1");
			order.setMessage("订购中，请稍后");
			order.setErrorMsg("订购中，请稍后");
			BsmOrderResultExecutor.service.setOrder(order);
			} 
		}else {
		 logger.info("ProvinderChoose statusInvoke() Method = "+m2);
		}
	   
	}

}
